package org.jiamandan.com.strategy.impl.car.listener;

import cn.hutool.core.date.DateUtil;
import io.netty.channel.Channel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.constant.YxConstant;
import org.jiamandan.com.dto.YXBasicDto;
import org.jiamandan.com.entity.RechargeGun;
import org.jiamandan.com.entity.RechargePile;
import org.jiamandan.com.service.IRechargeGunService;
import org.jiamandan.com.service.IRechargePileService;
import org.jiamandan.com.strategy.impl.car.IYXContextListener;
import org.jiamandan.com.utils.CommonUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 羽信实时充电数据
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Lee
 * @email:honvy.lee@qq.com
 * @see:
 * @创建日期：2023-03-24 14:40
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2023-03-24 14:40	         Lee        	创建
 * @end
 */
@Service(value = YxConstant.Service.CMD_13)
@AllArgsConstructor
@Slf4j
public class YxActualDataListener implements IYXContextListener {

    private static final int[] number = {32, 14, 2, 2, 2, 2, 4, 4, 2, 16, 2, 2, 4, 4, 8, 8, 8, 4};

    @Override
    public void doExecute(YXBasicDto dto, Channel client) throws Exception {
        log.info("接收JMD-YX充电枪实时数据，周期上送时，待机 5 分钟、充电 15 秒(CMD = 13)：" + dto);
        int cursor = 0, length = dto.getData().length();
        int start = 0, end = start + number[cursor];
        String orderNo = CommonUtil.removeZero(StringUtils.substring(dto.getData(), start, end), true);
        start = end;
        end += number[++cursor];
        String pileNo = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String gunNo = StringUtils.substring(dto.getData(), start, end);
        //TODO 可以判断充电桩是否在线
    }

    public static String secondConvertHourMinSecond(Integer second) {
        String str = "00:00:00";
        if (second == null || second < 0) {
            return str;
        }

        // 得到小时
        long h = second / 3600;
        str = h > 0 ? ((h < 10 ? ("0" + h) : h) + ":") : "00:";

        // 得到分钟
        long m = (second % 3600) / 60;
        str += (m < 10 ? ("0" + m) : m) + ":";

        //得到剩余秒
        long s = second % 60;
        str += (s < 10 ? ("0" + s) : s);
        return str;
    }
}
